/*
 * @Author: kangjia kjhangzhou@163.com
 * @Date: 2022-04-21 14:03:02
 * @LastEditors: wy
 * @LastEditTime: 2022-07-27 18:48:34
 * @FilePath: \boss-web-h5\utils\Me.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { getMeBuy } from '@/API/api.js'
import { groupExpTime } from '@/utils/index.js'
export default function (){
	const Me = {
		isShow: false, //是否已购买
		fail:false,//true表示请求成功
		type:0,//0 立即开通  1 延长使用
		message:{
			goodsName: "",
			expirationTime: "",
			color: ""
		}
	}
	const httpReturn = (res,successCallback,failCallback)=>{
		const { code, data} = res;
		if(code===2000){
			successCallback(data)
		}else{
			// 业务验证错误 ||  令牌错误  || 公共秘钥错误，请重新登录
			if(code===-91002 || code===-91003 || code===-91004 ){
		// 待定
			}
			failCallback(code)
		}
	}
	const handleMe = (item,successCallback) => {
		Me.fail = false;
		Me.isShow = false;
		getMeBuy(item).then(res => {
			httpReturn(res, (data) => {
				let datas = data || [];
				Me.isShow = datas.length > 0 ? true : false;
				Me.fail = true;//加载成功
				if(datas.length){
					datas.forEach((t) => {
						if (t.expirationTime) {
							const { color, time } = groupExpTime(t.expirationTime);//根据时间判断是否到期等
							t.color = color;
							t.expirationTime = time;
						} else {
							t.color = "primary";
							t.expirationTime = "";
						}
					});
					Me.type = datas[0].color ==='reds' ? 0 : 1;
					Me.message = datas[0];//已购信息
				}else{
					Me.type = 0;
				}
				successCallback(Me)
			}, (code) => {
				Me.fail = false;
			})
		}).catch(()=>{})
	}
	return {
		httpReturn,
		handleMe
	}
}